﻿using System;
using System.Windows.Controls;
using System.Windows;
using XAMLStylePad.Models;
using Microsoft.Win32;
using System.Collections.Generic;
using FireFly;

namespace XAMLStylePad.ViewModels
{
    public class MainWindowViewModel : ViewModelBase<XAMLStylePadMainWindow>
    {
        public MainWindowViewModel(XAMLStylePadMainWindow wnd)
            : base(wnd)
        {
        }

        #region overrides
        public override ContextMenu ContextMenu
        {
            get
            {
                return null;
            }
        }

        public override IRelayCommand SelectCommand
        {
            get
            {
                return null;
            }
        }

        public override string DisplayName
        {
            get
            {
                return "XAMLStylePad - " + Project.DisplayName;
            }
        }
        #endregion

        // Properties

        #region Project
        public ProjectViewModel Project
        {
            get
            {
                return _Project;
            }
            set
            {
                _Project = value;

                UpdateFileMenu();
                OnPropertyChanged("Project");
            }
        }
        ProjectViewModel _Project;
        #endregion

        #region CurrentItem
        public IViewModel CurrentItem
        {
            get
            {
                return _CurrentItem;
            }
            set
            {
                _CurrentItem = value;
                UpdateItemsMenu();
                OnPropertyChanged("CurrentItem");
            }
        }
        #endregion

        private void UpdateFileMenu()
        {
            _FileMenu.ItemsSource = new object[]{
                NewProjectCommand.CreateMenuItem(null, this),
                OpenProjectCommand.CreateMenuItem(null, this),
                ProjectViewModel.SaveCommand.CreateMenuItem(null, this.Project),
                //ProjectViewModel.LoadCommand.CreateMenuItem(null, this.Project),
                new Separator(),
                ProjectViewModel.AddXAMLSourceCommand.CreateMenuItem(null, this.Project),
                ProjectViewModel.AddAssemblySourceCommand.CreateMenuItem(null, this.Project),
                new Separator(),
                ExitCommand.CreateMenuItem(null, this)
            };
        }

        private void UpdateItemsMenu()
        {
            if(CurrentItem != null)
                _ItemsMenu.ItemsSource = ViewModelService.Instance.CreateMenuItems(CurrentItem, CurrentItem.Commands);
        }
        IViewModel _CurrentItem;

        MenuItem _ItemsMenu, _FileMenu;
      
        #region Menu
        public MenuItem[] Menu
        {
            get
            {
                if(_Menu == null)
                {
                    _FileMenu = new MenuItem()
                    {
                        Header = ViewModelService.Instance.GetString("Menu.File")
                    };
                    _ItemsMenu = new MenuItem()
                    {
                        Header = ViewModelService.Instance.GetString("Menu.Items")
                    };
                    UpdateFileMenu();
                    UpdateItemsMenu();

                    _Menu = new MenuItem[]{
                        _FileMenu,
                        _ItemsMenu,
                        new MenuItem()
                        {
                            Header = ViewModelService.Instance.GetString("Menu.Tools"),
                            ItemsSource = new object[]
                            {
                                MainWindowViewModel.SearchCommand.CreateMenuItem(null, this),
                                new Separator(),
                                MainWindowViewModel.OptionsCommand.CreateMenuItem(null, this)
                            }
                        },
                        new MenuItem()
                        {
                            Header = ViewModelService.Instance.GetString("Menu.Help"),
                            ItemsSource = new object[]
                            {
                                MainWindowViewModel.CheckUpdatesCommand.CreateMenuItem(null, this),
                                new Separator(),
                                MainWindowViewModel.AboutCommand.CreateMenuItem(null, this)
                            }
                        }
                    };
                }
                return _Menu;
            }
        }
        MenuItem[] _Menu;
        #endregion

        // Commands
        // project

        #region NewProjectCommand
        public static IRelayCommand NewProjectCommand { get { return _NewProjectCommand; } }
        static RelayCommand<MainWindowViewModel> _NewProjectCommand = new RelayCommand<MainWindowViewModel>(context =>
        {
            var vm = context.ViewModel;
            if(vm.Project != null && vm.Project.IsChanged)
            {
                var res = context.AppContext.ShowMessageBox("Last project is not saved. \nDo you want to save changes before create new project?", "Save project?", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                if(res == MessageBoxResult.Cancel)
                    return;
                else if(res == MessageBoxResult.Yes)
                    ProjectViewModel.SaveCommand.Execute(vm.Project);
            }

            var prj = new ProjectViewModel(new AppProject());

            if(vm.Project != null)
                vm.Project.Close();
            vm.Project = prj;

        }, "MainWindowViewModel.NewProjectCommand");
        #endregion

        #region OpenProjectCommand
        public IRelayCommand OpenProjectCommand { get { return _OpenProjectCommand; } }
        static RelayCommand<MainWindowViewModel> _OpenProjectCommand = new RelayCommand<MainWindowViewModel>(context =>
        {
            OpenFileDialog dlg = new OpenFileDialog()
            {
                Filter = "XAMLStylePad project files(*.spp)|*.spp",
                Multiselect = false,
                InitialDirectory = Environment.CurrentDirectory
            };

            if(dlg.ShowDialog() == true)
            {
                var prj = AppProject.Load(dlg.FileName);
                context.ViewModel.Project = new ProjectViewModel(prj);
            }
        }, "MainWindowViewModel.OpenProjectCommand");
        #endregion

        #region CloseProjectCommand
        public IRelayCommand CloseProjectCommand { get { return _CloseProjectCommand; } }
        static RelayCommand<MainWindowViewModel> _CloseProjectCommand = new EditCommand<MainWindowViewModel>(context =>
        {
            if(context.ViewModel.Project == null)
                return;
            context.ViewModel.Project.Close();
            context.ViewModel.Project = null;
            context.AppContext.PreviewPanel.ResetObject();
            context.AppContext.Tree.ItemsSource = null;
            context.AppContext.Editor.SetXAML(null);
        }, "MainWindowViewModel.CloseProjectCommand");
        #endregion

        // window

        #region ExitCommand
        public static IRelayCommand ExitCommand { get { return _ExitCommand; } }
        static RelayCommand<MainWindowViewModel> _ExitCommand = new RelayCommand<MainWindowViewModel>(context =>
        {
            var wnd = context.ViewModel.Model;
            if(context.ViewModel.Project.IsChanged)
            {
                if(context.AppContext.ShowMessageBox("You project was changed. Do you want to save it?", "Save changes...", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    ProjectViewModel.SaveCommand.Execute(context.ViewModel.Project);
                }
            }
            wnd.Close();

        }, "MainWindowViewModel.ExitCommand");
        #endregion

        #region SearchCommand
        public static IRelayCommand SearchCommand { get { return _SearchCommand; } }
        static RelayCommand<MainWindowViewModel> _SearchCommand = new RelayCommand<MainWindowViewModel>(context =>
        {
            context.AppContext.ShowMessageBox("Search is not implemented.");
        }, "MainWindowViewModel.SearchCommand");
        #endregion

        #region OptionsCommand
        public static IRelayCommand OptionsCommand { get { return _OptionsCommand; } }
        static RelayCommand<MainWindowViewModel> _OptionsCommand = new RelayCommand<MainWindowViewModel>(context =>
        {
            context.AppContext.ShowMessageBox("Not implemented");
        }, "MainWindowViewModel.OptionsCommand");
        #endregion

        #region AboutCommand
        public static IRelayCommand AboutCommand { get { return _AboutCommand; } }
        static IRelayCommand _AboutCommand = new RelayCommand<MainWindowViewModel>(context =>
        {
            context.AppContext.ShowMessageBox("Not implemented");
        }, "MainWindowViewModel.AboutCommand");
        #endregion

        #region CheckUpdatesCommand
        public static IRelayCommand CheckUpdatesCommand { get { return _CheckUpdatesCommand; } }
        static RelayCommand<MainWindowViewModel> _CheckUpdatesCommand = new RelayCommand<MainWindowViewModel>(context =>
        {
            context.AppContext.ShowMessageBox("Not implemented.");
        }, "MainWindowViewModel.CheckUpdatesCommand");
        #endregion
    }
}
